package com.yilin.tms.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.order.party.OrderAppraise;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.order.service.order.ITransitService;
import com.yilin.tms.order.service.party.IOrderAppraiseService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * 运单评价控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/31
 */
@RestController
@RequestMapping("/order/appraise/")
public class AppraiseController extends BaseController {

    @Resource
    IOrderAppraiseService orderAppraiseService;
    @Resource
    ITransitService transitService;

    /**
     * 获取评价分页
     */
    @PostMapping(value = "getOrganizeAppraisePage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<OrderAppraise>> getOrganizeAppraisePage(OrderAppraise query,String organizeId, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        if (organizeId == null && getLoginUser().getUserType().index > UserType.platformUser.index) return jsonErrView("请提供您要查询的组织标识！");
        PageData<OrderAppraise> pageData = orderAppraiseService.getOrganizeAppraisePage(getLoginUser(),organizeId, query, page, limit);
        return jsonView(SUCCESS_CODE, "获取成功", pageData);
    }

    /**
     * 获取最近前十条评价
     */
    @PostMapping(value = "getTopTenAppraiseList")
    @NotNull({"organizeId"})
    public ReturnData<List<OrderAppraise>> getTopTenAppraiseList(String organizeId) {
        List<OrderAppraise> pageData = orderAppraiseService.getTopTenAppraiseList(organizeId);
        return jsonView(SUCCESS_CODE, "获取成功", pageData);
    }

    /**
     * 根据运单获取运单评价
     */
    @PostMapping(value = "getAppraiseByOrderId")
    @NotNull("transitOrderId")
    public ReturnData<OrderAppraise> getAppraiseByOrderId(@Explain("运单ID") String transitOrderId) {
        TransitOrder transitOrder = transitService.getById(transitOrderId);
        ParamUtil.isNull(transitOrder, "未查询到运单");
        ParamUtil.notEquals("运单还没有评价", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.complete
                , TransitOrder.TransitStatus.waitingSettlement);
        OrderAppraise orderAppraise = orderAppraiseService.getOne(new QueryWrapper<OrderAppraise>()
                .eq("transit_order_id", transitOrderId));
        ParamUtil.isNull(orderAppraise, "运单还未评价");
        return jsonView(true, "获取成功", orderAppraise);
    }

    /**
     * 保存运单评价
     */
    @PostMapping(value = "saveAppraise")
    @NotNull({"transitOrderId", "appraiseGrade", "appraiseText"})
    @UserTarget(UserType.shipperUser)
    public ReturnData<Object> saveAppraise(OrderAppraise partyOrderAppraise) {
        TransitOrder transitOrder = transitService.getById(partyOrderAppraise.getTransitOrderId());
        ParamUtil.isNull(transitOrder, "未知的运单");
        ParamUtil.notEquals("只能评价完成的运单", transitOrder.getTransitStatus(), TransitOrder.TransitStatus.complete,
                TransitOrder.TransitStatus.waitingSettlement);
        if (transitOrder.getAlreadyAppraise()) return jsonView(false, "该运单已评价过");
        ParamUtil.notEquals("你无权评价该运单", getLoginUser().getId(), transitOrder.getShipperAccountId());
        OrderAppraise localPartyOrderAppraise = orderAppraiseService.getOne(new QueryWrapper<OrderAppraise>()
                .eq("transit_order_id", transitOrder.getId()));
        if (localPartyOrderAppraise!=null) return jsonView(FAIL_CODE, "该运单已评价");
        //送达操作时限自动评价
        long finallyTime = transitOrder.getReceiptTime().getTime() + (86400000 * 7);
        long currentTime = System.currentTimeMillis();
        if (currentTime > finallyTime) return jsonView(false, "超过最后评价时间");
        orderAppraiseService.saveOrderAppraise(getLoginUser(), partyOrderAppraise, transitOrder);
        return jsonView(SUCCESS_CODE, "评价成功", null);
    }

    /**
     * 批量修改评价状态
     */
    @PostMapping(value = "updateAuditStateList")
    @NotNull({"appraiseId", "auditState"})
    @UserTarget({UserType.adminUser,UserType.platformUser})
    public ReturnData<Object> updateAuditStateList(@Explain("集合") String[] appraiseIds,@Explain("状态") OrderAppraise.AuditState auditState) {
        orderAppraiseService.updateAuditStateList(appraiseIds,auditState);
        return jsonView(SUCCESS_CODE, "评价成功");
    }


}
